/*
 * Copyright (C) 2018 justlive1
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vip.justlive.oxygen.web.http;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

/**
 * Cookie解析器
 *
 * @author wubo
 */
public class CookieRequestParse extends AbstractRequestParse {

  @Override
  public boolean supported(HttpServletRequest req) {
    return req.getCookies() != null;
  }

  @Override
  public void handle(HttpServletRequest req) {
    javax.servlet.http.Cookie[] cookies = req.getCookies();
    Request request = Request.current();
    if (request.getCookies() == null) {
      request.setCookies(new HashMap<>(4));
    }
    for (javax.servlet.http.Cookie cookie : cookies) {
      Cookie vipCookie = new Cookie();
      vipCookie.setDomain(cookie.getDomain());
      vipCookie.setMaxAge(cookie.getMaxAge());
      vipCookie.setName(cookie.getName());
      vipCookie.setPath(cookie.getPath());
      vipCookie.setValue(cookie.getValue());
      request.getCookies().put(vipCookie.getName(), vipCookie);
    }
  }
}
